home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Games / MAME / src / sound / upd7759.c < prev    next >
C/C++ Source or Header  |  2000-04-23  |  25KB  |  828 lines

  1. /************************************************************
  2.  
  3.  NEC uPD7759 ADPCM Speech Processor
  4.  by: Juergen Buchmueller, Mike Balfour and Howie Cohen
  5.  
  6.  
  7.  Description:
  8.  The uPD7759 is a speech processing LSI that, with an external
  9.  ROM, utilizes ADPCM to produce speech.  The uPD7759 can
  10.  directly address up to 1Mbits of external data ROM, or the
  11.  host CPU can control the speech data transfer.  Three sample
  12.  frequencies are selectable - 5, 6, or 8 kHz.  The external
  13.  ROM can store a maximum of 256 different messages and up to
  14.  50 seconds of speech.
  15.  
  16.  The uPD7759 should always be hooked up to a 640 kHz clock.
  17.  
  18.  TODO:
  19.  1) find bugs
  20.  2) fix bugs
  21.  3) Bankswitching and frequency selection may not be 100%
  22.  
  23. NOTES:
  24.  
  25. There are 2 types of upd7759 sound roms, master and slave.
  26.  
  27. A master rom has a header at the beginning of the rom
  28. for example : 15 5A A5 69 55 (this is the POW header)
  29.  
  30. -the 1st byte (15) is the number of samples stored in the rom
  31.  (actually the number of samples minus 1 - NS)
  32. -the next 4 bytes seems standard in every upd7759 rom used in
  33. master mode (5A A5 69 55)
  34. -after that there is table of (sample offsets)/2 we use this to
  35. calculate the sample table on the fly. Then the samples start,
  36. each sample has a short header with sample rate info in it.
  37. A master rom can have up to 256 samples , and there should be
  38. only one rom per upd7759.
  39.  
  40. a slave rom has no header... but each sample starts with
  41. FF 00 00 00 00 10 (followed by a few other bytes that are
  42. usually the same but not always)
  43.  
  44. Clock rates:
  45. in master mode the clock rate should always be 64000000
  46. sample frequencies are selectable - 5, 6, or 8 kHz. This
  47. info is coded in the uPD7759 roms, it selects the sample
  48. frequency for you.
  49.  
  50. slave mode is still some what of a mystery.  Everything
  51. we know about slave mode (and 1/2 of everything in master
  52. mode) is from guesswork. As far as we know the clock rate
  53. is the same for all samples in slave mode on a per game basis
  54. so valid clock rates are: 40000000, 48000000, 64000000
  55.  
  56. Differances between master/slave mode.
  57. (very basic explanation based on my understanding)
  58.  
  59. Master mode: the sound cpu sends a sample number to the upd7759
  60. it then sends a trigger command to it, and the upd7759 plays the
  61. sample directly from the rom (like an adpcm chip)
  62.  
  63. Slave mode:  the sound cpu sends data to the upd7759 to select
  64. the bank for the sound data, each bank is 0x4000 in size, and
  65. the sample offset. The sound cpu then sends the data for the sample
  66. a byte(?) at a time (dac / cvsd like) which the upd7759 plays till
  67. it reaches the header of the next sample (FF 00 00 00 00)
  68.  
  69. Changes:
  70. 05/99    HJB
  71.     Tried to figure better index_shift and diff_lookutp tables and
  72.     also adjusted sample value range. It seems the 4 bits of the
  73.     ADPCM data are signed (0x0f == -1)! Also a signal and step
  74.     width fall off seems to be closer to the real thing.
  75.     Reduced work load by adding a wrap around buffer for slave
  76.     mode data that is stuffed by the sound CPU.
  77.     Finally removed (now obsolete) 8 bit sample support.
  78.  
  79.  *************************************************************/
  80.  
  81. #include <stdio.h>
  82. #include <stdlib.h>
  83. #include <math.h>
  84.  
  85. #include "driver.h"
  86. #include "upd7759.h"
  87. #include "streams.h"
  88.  
  89. #define VERBOSE 0
  90.  
  91. #if VERBOSE
  92. #define LOG(n,x)  if((n)>=VERBOSE) logerror x
  93. #else
  94. #define LOG(n,x)
  95. #endif
  96.  
  97. /* number of samples stuffed into the rom */
  98. static unsigned char numsam;
  99.  
  100. /* playback rate for the streams interface */
  101. /* BASE_CLOCK or a multiple (if oversampling is active) */
  102. static int emulation_rate;
  103.  
  104. static int base_rate;
  105. /* define the output rate */
  106. #define CLOCK_DIVIDER    80
  107.  
  108. #define OVERSAMPLING    0    /* 1 use oversampling, 0 don't */
  109.  
  110. /* signal fall off factor */
  111. #define FALL_OFF(n)     ((n)-(((n)+7)/8))
  112.  
  113. #define SIGNAL_BITS     15    /* signal range */
  114. #define SIGNAL_MAX        (0x7fff >> (15-SIGNAL_BITS))
  115. #define SIGNAL_MIN        -SIGNAL_MAX
  116.  
  117. #define STEP_MAX        32
  118. #define STEP_MIN        0
  119.  
  120. #define DATA_MAX        512
  121.  
  122. struct UPD7759sample
  123. {
  124.     unsigned int offset;    /* offset in that region */
  125.     unsigned int length;    /* length of the sample */
  126.     unsigned int freq;        /* play back freq of sample */
  127. };
  128.  
  129.  
  130. /* struct describing a single playing ADPCM voice */
  131. struct UPD7759voice
  132. {
  133.     int playing;            /* 1 if we are actively playing */
  134.     unsigned char *base;    /* pointer to the base memory location */
  135.     int mask;               /* mask to keep us within the buffer */
  136.     int sample;             /* current sample number (sample data in slave mode) */
  137.     int freq;                /* current sample playback freq */
  138.     int count;              /* total samples to play */
  139.     int signal;             /* current ADPCM signal */
  140. #if OVERSAMPLING
  141.     int old_signal;         /* last ADPCM signal */
  142. #endif
  143.     int step;               /* current ADPCM step */
  144.     int counter;            /* sample counter */
  145.     void *timer;            /* timer used in slave mode */
  146.     int data[DATA_MAX];     /* data array used in slave mode */
  147.     unsigned head;            /* head of data array used in slave mode */
  148.     unsigned tail;            /* tail of data array used in slave mode */
  149.     unsigned available;
  150. };
  151.  
  152. /* global pointer to the current interface */
  153. static const struct UPD7759_interface *upd7759_intf;
  154.  
  155. /* array of ADPCM voices */
  156. static struct UPD7759voice updadpcm[MAX_UPD7759];
  157.  
  158. #if OVERSAMPLING
  159. /* oversampling factor, ie. playback_rate / BASE_CLOCK */
  160. static int oversampling;
  161. #endif
  162. /* array of channels returned by streams.c */
  163. static int channel[MAX_UPD7759];
  164.  
  165. /* stores the current sample number */
  166. static int sampnum[MAX_UPD7759];
  167.  
  168. /* step size index shift table */
  169. #define INDEX_SHIFT_MAX 16
  170. static int index_shift[INDEX_SHIFT_MAX] = {
  171.     0,     1,  2,  3,  6,  7, 10, 15,
  172.     0,  15, 10,  7,  6,  3,  2,  1
  173. };
  174.  
  175. /* lookup table for the precomputed difference */
  176. static int diff_lookup[(STEP_MAX+1)*16];
  177.  
  178. static void UPD7759_update (int chip, INT16 *buffer, int left);
  179.  
  180. /*
  181.  *   Compute the difference table
  182.  */
  183.  
  184. static void ComputeTables (void)
  185. {
  186.     /* nibble to bit map */
  187.     static int nbl2bit[16][4] = {
  188.         { 1, 0, 0, 0}, { 1, 0, 0, 1}, { 1, 0, 1, 0}, { 1, 0, 1, 1},
  189.         { 1, 1, 0, 0}, { 1, 1, 0, 1}, { 1, 1, 1, 0}, { 1, 1, 1, 1},
  190.         {-1, 0, 0, 0}, {-1, 0, 0, 1}, {-1, 0, 1, 0}, {-1, 0, 1, 1},
  191.         {-1, 1, 0, 0}, {-1, 1, 0, 1}, {-1, 1, 1, 0}, {-1, 1, 1, 1},
  192.     };
  193.     int step, nib;
  194.  
  195.     /* loop over all possible steps */
  196.     for (step = 0; step <= STEP_MAX; step++)
  197.     {
  198.         /* compute the step value */
  199.         int stepval = 6 * (step+1) * (step+1);
  200.         LOG(1,("step %2d:", step));
  201.         /* loop over all nibbles and compute the difference */
  202.         for (nib = 0; nib < 16; nib++)
  203.         {
  204.             diff_lookup[step*16 + nib] = nbl2bit[nib][0] *
  205.                 (stepval   * nbl2bit[nib][1] +
  206.                  stepval/2 * nbl2bit[nib][2] +
  207.                  stepval/4 * nbl2bit[nib][3] +
  208.                  stepval/8);
  209.             LOG(1,(" %+6d", diff_lookup[step*16 + nib]));
  210.         }
  211.         LOG(1,("\n"));
  212.     }
  213. }
  214.  
  215.  
  216.  
  217. static int find_sample(int num, int sample_num,struct UPD7759sample *sample)
  218. {
  219.     int j;
  220.     int nextoff = 0;
  221.     unsigned char *memrom;
  222.     unsigned char *header;   /* upd7759 has a 4 byte what we assume is an identifier (bytes 1-4)*/
  223.     unsigned char *data;
  224.  
  225.  
  226.     memrom = memory_region(upd7759_intf->region[num]);
  227.  
  228.     numsam = (unsigned int)memrom[0]; /* get number of samples from sound rom */
  229.     header = &(memrom[1]);
  230.  
  231.     if (memcmp (header, "\x5A\xA5\x69\x55",4) == 0)
  232.     {
  233.         LOG(1,("uPD7759 header verified\n"));
  234.     }
  235.     else
  236.     {
  237.         LOG(1,("uPD7759 header verification failed\n"));
  238.     }
  239.  
  240.     LOG(1,("Number of samples in UPD7759 rom = %d\n",numsam));
  241.  
  242.     /* move the header pointer to the start of the sample offsets */
  243.     header = &(memrom[5]);
  244.  
  245.  
  246.     if (sample_num > numsam) return 0;    /* sample out of range */
  247.  
  248.  
  249.     nextoff = 2 * sample_num;
  250.     sample->offset = ((((unsigned int)(header[nextoff]))<<8)+(header[nextoff+1]))*2;
  251.     data = &memory_region(upd7759_intf->region[num])[sample->offset];
  252.     /* guesswork, probably wrong */
  253.     j = 0;
  254.     if (!data[j]) j++;
  255.     if ((data[j] & 0xf0) != 0x50) j++;
  256.  
  257.     // Added and Modified by Takahiro Nogi. 1999/10/28
  258. #if 0    // original
  259.     switch (data[j])
  260.     {
  261.         case 0x53: sample->freq = 8000; break;
  262.         case 0x59: sample->freq = 6000; break;
  263.         case 0x5f: sample->freq = 5000; break;
  264.         default:
  265.             sample->freq = 5000;
  266.     }
  267. #else    // modified by Takahiro Nogi. 1999/10/28
  268.     switch (data[j] & 0x1f)
  269.     {
  270.         case 0x13: sample->freq = 8000; break;
  271.         case 0x19: sample->freq = 6000; break;
  272.         case 0x1f: sample->freq = 5000; break;
  273.         default:                // ???
  274.             sample->freq = 5000;
  275.     }
  276. #endif
  277.  
  278.     if (sample_num == numsam)
  279.     {
  280.         sample->length = 0x20000 - sample->offset;
  281.     }
  282.     else
  283.         sample->length = ((((unsigned int)(header[nextoff+2]))<<8)+(header[nextoff+3]))*2 -
  284.                             ((((unsigned int)(header[nextoff]))<<8)+(header[nextoff+1]))*2;
  285.  
  286.     data = &memory_region(upd7759_intf->region[num])[sample->offset];
  287.     logerror("play sample %3d, offset $%06x, length %5d, freq = %4d [data $%02x $%02x $%02x]\n",
  288.         sample_num,
  289.         sample->offset,
  290.         sample->length,
  291.         sample->freq,
  292.         data[0],data[1],data[2]);
  293.  
  294.     return 1;
  295. }
  296.  
  297.  
  298. /*
  299.  *   Start emulation of several ADPCM output streams
  300.  */
  301.  
  302.  
  303. int UPD7759_sh_start (const struct MachineSound *msound)
  304. {
  305.     int i;
  306.     const struct UPD7759_interface *intf = msound->sound_interface;
  307.  
  308.     if( Machine->sample_rate == 0 )
  309.         return 0;
  310.  
  311.     /* compute the difference tables */
  312.     ComputeTables ();
  313.  
  314.     /* copy the interface pointer to a global */
  315.     upd7759_intf = intf;
  316.     base_rate = intf->clock_rate / CLOCK_DIVIDER;
  317.  
  318. #if OVERSAMPLING
  319.     oversampling = (Machine->sample_rate / base_rate);
  320.     if (!oversampling) oversampling = 1;
  321.     emulation_rate = base_rate * oversampling;
  322. #else
  323.     emulation_rate = base_rate;
  324. #endif
  325.  
  326.  
  327.     memset(updadpcm,0,sizeof(updadpcm));
  328.     for (i = 0; i < intf->num; i++)
  329.     {
  330.         char name[20];
  331.  
  332.         updadpcm[i].mask = 0xffffffff;
  333.         updadpcm[i].signal = 0;
  334.         updadpcm[i].step = 0;
  335.         updadpcm[i].counter = emulation_rate / 2;
  336.  
  337.         sprintf(name,"uPD7759 #%d",i);
  338.  
  339.         channel[i] = stream_init(name,intf->volume[i],emulation_rate,i,UPD7759_update);
  340.     }
  341.     return 0;
  342. }
  343.  
  344.  
  345. /*
  346.  *   Stop emulation of several UPD7759 output streams
  347.  */
  348.  
  349. void UPD7759_sh_stop (void)
  350. {
  351. }
  352.  
  353.  
  354. /*
  355.  *   Update emulation of an uPD7759 output stream
  356.  */
  357. static void UPD7759_update (int chip, INT16 *buffer, int left)
  358. {
  359.     struct UPD7759voice *voice = &updadpcm[chip];
  360.     int i;
  361.  
  362.     /* see if there's actually any need to generate samples */
  363.     LOG(3,("UPD7759_update %d (%d)\n", left, voice->available));
  364.  
  365.     if (left > 0)
  366.     {
  367.         /* if this voice is active */
  368.         if (voice->playing)
  369.         {
  370.             voice->available -= left;
  371.             if( upd7759_intf->mode == UPD7759_SLAVE_MODE )
  372.             {
  373.                 while( left-- > 0 )
  374.                 {
  375.                     *buffer++ = voice->data[voice->tail];
  376. #if OVERSAMPLE
  377.                     if( (voice->counter++ % OVERSAMPLE) == 0 )
  378. #endif
  379.                     voice->tail = (voice->tail + 1) % DATA_MAX;
  380.                 }
  381.             }
  382.             else
  383.             {
  384.                 unsigned char *base = voice->base;
  385.                 int val;
  386. #if OVERSAMPLING
  387.                 int i, delta;
  388. #endif
  389.  
  390.                 while( left > 0 )
  391.                 {
  392.                     /* compute the new amplitude and update the current voice->step */
  393.                     val = base[(voice->sample / 2) & voice->mask] >> (((voice->sample & 1) << 2) ^ 4);
  394.                     voice->step = FALL_OFF(voice->step) + index_shift[val & (INDEX_SHIFT_MAX-1)];
  395.                     if (voice->step > STEP_MAX) voice->step = STEP_MAX;
  396.                     else if (voice->step < STEP_MIN) voice->step = STEP_MIN;
  397.                     voice->signal = FALL_OFF(voice->signal) + diff_lookup[voice->step * 16 + (val & 15)];
  398.                     if (voice->signal > SIGNAL_MAX) voice->signal = SIGNAL_MAX;
  399.                     else if (voice->signal < SIGNAL_MIN) voice->signal = SIGNAL_MIN;
  400. #if OVERSAMPLING
  401.                     i = 0;
  402.                     delta = voice->signal - voice->old_signal;
  403.                     while (voice->counter > 0 && left > 0)
  404.                     {
  405.                         *sample++ = voice->old_signal + delta * i / oversampling;
  406.                         if (++i == oversampling) i = 0;
  407.                         voice->counter -= voice->freq;
  408.                         left--;
  409.                     }
  410.                     voice->old_signal = voice->signal;
  411. #else
  412.                     while (voice->counter > 0 && left > 0)
  413.                     {
  414.                         *buffer++ = voice->signal;
  415.                         voice->counter -= voice->freq;
  416.                         left--;
  417.                     }
  418. #endif
  419.                     voice->counter += emulation_rate;
  420.  
  421.                     /* next! */
  422.                     if( ++voice->sample > voice->count )
  423.                     {
  424.                         while (left-- > 0)
  425.                         {
  426.                             *buffer++ = voice->signal;
  427.                             voice->signal = FALL_OFF(voice->signal);
  428.                         }
  429.                         voice->playing = 0;
  430.                         break;
  431.                     }
  432.                 }
  433.             }
  434.         }
  435.         else
  436.         {
  437.             /* voice is not playing */
  438.             for (i = 0; i < left; i++)
  439.                 *buffer++ = voice->signal;
  440.         }
  441.     }
  442. }
  443.  
  444. /************************************************************
  445.  UPD7759_message_w
  446.  
  447.  Store the inputs to I0-I7 externally to the uPD7759.
  448.  
  449.  I0-I7 input the message number of the message to be
  450.  reproduced. The inputs are latched at the rising edge of the
  451.  !ST input. Unused pins should be grounded.
  452.  
  453.  In slave mode it seems like the ADPCM data is stuffed
  454.  here from an external source (eg. Z80 NMI code).
  455.  *************************************************************/
  456.  
  457. void UPD7759_message_w (int num, int data)
  458. {
  459.     struct UPD7759voice *voice = updadpcm + num;
  460.  
  461.     /* bail if we're not playing anything */
  462.     if (Machine->sample_rate == 0)
  463.         return;
  464.  
  465.     /* range check the numbers */
  466.     if( num >= upd7759_intf->num )
  467.     {
  468.         LOG(1,("error: UPD7759_SNDSELECT() called with channel = %d, but only %d channels allocated\n", num, upd7759_intf->num));
  469.         return;
  470.     }
  471.  
  472.     if (upd7759_intf->mode == UPD7759_SLAVE_MODE)
  473.     {
  474.         int offset = -1;
  475.  
  476.         //LOG(1,("upd7759_message_w $%02x\n", data));
  477.         logerror("upd7759_message_w $%2x\n",data);
  478.  
  479.         switch (data) {
  480.  
  481.             case 0x00:                             /* roms 0x10000 & 0x20000 in size */
  482.             case 0x38: offset = 0x10000; break; /* roms 0x8000 in size */
  483.  
  484.             case 0x01:                             /* roms 0x10000 & 0x20000 in size */
  485.             case 0x39: offset = 0x14000; break; /* roms 0x8000 in size */
  486.  
  487.             case 0x02:                             /* roms 0x10000 & 0x20000 in size */
  488.             case 0x34: offset = 0x18000; break; /* roms 0x8000 in size */
  489.  
  490.             case 0x03:                             /* roms 0x10000 & 0x20000 in size */
  491.             case 0x35: offset = 0x1c000; break; /* roms 0x8000 in size */
  492.  
  493.             case 0x04:                            /* roms 0x10000 & 0x20000 in size */
  494.             case 0x2c: offset = 0x20000; break; /* roms 0x8000 in size */
  495.  
  496.             case 0x05:                             /* roms 0x10000 & 0x20000 in size */
  497.             case 0x2d: offset = 0x24000; break; /* roms 0x8000 in size */
  498.  
  499.             case 0x06:                            /* roms 0x10000 & 0x20000 in size */
  500.             case 0x1c: offset = 0x28000; break;    /* roms 0x8000 in size in size */
  501.  
  502.             case 0x07:                             /* roms 0x10000 & 0x20000 in size */
  503.             case 0x1d: offset = 0x2c000; break;    /* roms 0x8000 in size */
  504.  
  505.             case 0x08: offset = 0x30000; break; /* roms 0x10000 & 0x20000 in size */
  506.             case 0x09: offset = 0x34000; break; /* roms 0x10000 & 0x20000 in size */
  507.             case 0x0a: offset = 0x38000; break; /* roms 0x10000 & 0x20000 in size */
  508.             case 0x0b: offset = 0x3c000; break; /* roms 0x10000 & 0x20000 in size */
  509.             case 0x0c: offset = 0x40000; break; /* roms 0x10000 & 0x20000 in size */
  510.             case 0x0d: offset = 0x44000; break; /* roms 0x10000 & 0x20000 in size */
  511.             case 0x0e: offset = 0x48000; break; /* roms 0x10000 & 0x20000 in size */
  512.             case 0x0f: offset = 0x4c000; break; /* roms 0x10000 & 0x20000 in size */
  513.  
  514.             default:
  515.  
  516.                 //LOG(1,("upd7759_message_w unhandled $%02x\n", data));
  517.                 logerror("upd7759_message_w unhandled $%02x\n", data);
  518.                 if ((data & 0xc0) == 0xc0)
  519.                 {
  520.                     if (voice->timer)
  521.                     {
  522.                         timer_remove(voice->timer);
  523.                         voice->timer = 0;
  524.                     }
  525.                     voice->playing = 0;
  526.                 }
  527.         }
  528.         if (offset > 0)
  529.         {
  530.             voice->base = &memory_region(upd7759_intf->region[num])[offset];
  531.             //LOG(1,("upd7759_message_w set base $%08x\n", offset));
  532.             logerror("upd7759_message_w set base $%08x\n", offset);
  533.         }
  534.     }
  535.     else
  536.     {
  537.  
  538.         LOG(1,("uPD7759 calling sample : %d\n", data));
  539.         sampnum[num] = data;
  540.  
  541.     }
  542. }
  543.  
  544. /************************************************************
  545.  UPD7759_dac
  546.  
  547.  Called by the timer interrupt at twice the sample rate.
  548.  The first time the external irq callback is called, the
  549.  second time the ADPCM msb is converted and the resulting
  550.  signal is sent to the DAC.
  551.  ************************************************************/
  552. static void UPD7759_dac(int num)
  553. {
  554.     static int dac_msb = 0;
  555.     struct UPD7759voice *voice = updadpcm + num;
  556.  
  557.     dac_msb ^= 1;
  558.     if( dac_msb )
  559.     {
  560.         LOG(3,("UPD7759_dac:    $%x ", voice->sample & 15));
  561.         /* convert lower nibble */
  562.         voice->step = FALL_OFF(voice->step) + index_shift[voice->sample & (INDEX_SHIFT_MAX-1)];
  563.         if (voice->step > STEP_MAX) voice->step = STEP_MAX;
  564.         else if (voice->step < STEP_MIN) voice->step = STEP_MIN;
  565.         voice->signal = FALL_OFF(voice->signal) + diff_lookup[voice->step * 16 + (voice->sample & 15)];
  566.         if (voice->signal > SIGNAL_MAX) voice->signal = SIGNAL_MAX;
  567.         else if (voice->signal < SIGNAL_MIN) voice->signal = SIGNAL_MIN;
  568.         LOG(3,("step: %3d signal: %+5d\n", voice->step, voice->signal));
  569.         voice->head = (voice->head + 1) % DATA_MAX;
  570.         voice->data[voice->head] = voice->signal;
  571.         voice->available++;
  572.     }
  573.     else
  574.     {
  575.         if( upd7759_intf->irqcallback[num] )
  576.             (*upd7759_intf->irqcallback[num])(num);
  577.     }
  578. }
  579.  
  580. /************************************************************
  581.  UPD7759_start_w
  582.  
  583.  !ST pin:
  584.  Setting the !ST input low while !CS is low will start
  585.  speech reproduction of the message in the speech ROM locations
  586.  addressed by the contents of I0-I7. If the device is in
  587.  standby mode, standby mode will be released.
  588.  NOTE: While !BUSY is low, another !ST will not be accepted.
  589.  *************************************************************/
  590.  
  591. void UPD7759_start_w (int num, int data)
  592. {
  593.     struct UPD7759voice *voice = updadpcm + num;
  594.  
  595.     /* bail if we're not playing anything */
  596.     if (Machine->sample_rate == 0)
  597.         return;
  598.  
  599.     /* range check the numbers */
  600.     if( num >= upd7759_intf->num )
  601.     {
  602.         LOG(1,("error: UPD7759_play_stop() called with channel = %d, but only %d channels allocated\n", num, upd7759_intf->num));
  603.         return;
  604.     }
  605.  
  606.     /* handle the slave mode */
  607.     if (upd7759_intf->mode == UPD7759_SLAVE_MODE)
  608.     {
  609.         if (voice->playing)
  610.         {
  611.             /* if the chip is busy this should be the ADPCM data */
  612.             data &= 0xff;    /* be sure to use 8 bits value only */
  613.             LOG(3,("UPD7759_data_w: $%x ", (data >> 4) & 15));
  614.  
  615.             /* detect end of a sample by inspection of the last 5 bytes */
  616.             /* FF 00 00 00 00 is the start of the next sample */
  617.             if( voice->count > 5 && voice->sample == 0xff && data == 0x00 )
  618.             {
  619.                 /* remove an old timer */
  620.                 if (voice->timer)
  621.                 {
  622.                     timer_remove(voice->timer);
  623.                     voice->timer = 0;
  624.                 }
  625.                 /* stop playing this sample */
  626.                 voice->playing = 0;
  627.                 return;
  628.             }
  629.  
  630.             /* save the data written in voice->sample */
  631.             voice->sample = data;
  632.             voice->count++;
  633.  
  634.             /* conversion of the ADPCM data to a new signal value */
  635.             voice->step = FALL_OFF(voice->step) + index_shift[(voice->sample >> 4) & (INDEX_SHIFT_MAX-1)];
  636.             if (voice->step > STEP_MAX) voice->step = STEP_MAX;
  637.             else if (voice->step < STEP_MIN) voice->step = STEP_MIN;
  638.             voice->signal = FALL_OFF(voice->signal) + diff_lookup[voice->step * 16 + ((voice->sample >> 4) & 15)];
  639.             if (voice->signal > SIGNAL_MAX) voice->signal = SIGNAL_MAX;
  640.             else if (voice->signal < SIGNAL_MIN) voice->signal = SIGNAL_MIN;
  641.             LOG(3,("step: %3d signal: %+5d\n", voice->step, voice->signal));
  642.             voice->head = (voice->head + 1) % DATA_MAX;
  643.             voice->data[voice->head] = voice->signal;
  644.             voice->available++;
  645.         }
  646.         else
  647.         {
  648.             LOG(2,("UPD7759_start_w: $%02x\n", data));
  649.             /* remove an old timer */
  650.             if (voice->timer)
  651.             {
  652.                 timer_remove(voice->timer);
  653.                 voice->timer = 0;
  654.             }
  655.             /* bring the chip in sync with the CPU */
  656.             stream_update(channel[num], 0);
  657.             /* start a new timer */
  658.             voice->timer = timer_pulse( TIME_IN_HZ(base_rate), num, UPD7759_dac );
  659.             voice->signal = 0;
  660.             voice->step = 0;    /* reset the step width */
  661.             voice->count = 0;    /* reset count for the detection of an sample ending */
  662.             voice->playing = 1; /* this voice is now playing */
  663.             voice->tail = 0;
  664.             voice->head = 0;
  665.             voice->available = 0;
  666.         }
  667.     }
  668.     else
  669.     {
  670.         struct UPD7759sample sample;
  671.  
  672.         /* if !ST is high, do nothing */ /* EHC - 13/08/99 */
  673.         if (data > 0)
  674.             return;
  675.  
  676.         /* bail if the chip is busy */
  677.         if (voice->playing)
  678.             return;
  679.  
  680.         LOG(2,("UPD7759_start_w: %d\n", data));
  681.  
  682.         /* find a match */
  683.         if (find_sample(num,sampnum[num],&sample))
  684.         {
  685.             /* update the  voice */
  686.             stream_update(channel[num], 0);
  687.             voice->freq = sample.freq;
  688.             /* set up the voice to play this sample */
  689.             voice->playing = 1;
  690.             voice->base = &memory_region(upd7759_intf->region[num])[sample.offset];
  691.             voice->sample = 0;
  692.             /* sample length needs to be doubled (counting nibbles) */
  693.             voice->count = sample.length * 2;
  694.  
  695.             /* also reset the chip parameters */
  696.             voice->step = 0;
  697.             voice->counter = emulation_rate / 2;
  698.  
  699.             return;
  700.         }
  701.  
  702.         LOG(1,("warning: UPD7759_playing_w() called with invalid number = %08x\n",data));
  703.     }
  704. }
  705.  
  706. /************************************************************
  707.  UPD7759_data_r
  708.  
  709.  External read data from the UPD7759 memory region based
  710.  on voice->base. Used in slave mode to retrieve data to
  711.  stuff into UPD7759_message_w.
  712.  *************************************************************/
  713.  
  714. int UPD7759_data_r(int num, int offs)
  715. {
  716.     struct UPD7759voice *voice = updadpcm + num;
  717.  
  718.     /* If there's no sample rate, do nothing */
  719.     if (Machine->sample_rate == 0)
  720.         return 0x00;
  721.  
  722.     /* range check the numbers */
  723.     if( num >= upd7759_intf->num )
  724.     {
  725.         LOG(1,("error: UPD7759_data_r() called with channel = %d, but only %d channels allocated\n", num, upd7759_intf->num));
  726.         return 0x00;
  727.     }
  728.  
  729.     if ( voice->base == NULL )
  730.     {
  731.         LOG(1,("error: UPD7759_data_r() called with channel = %d, but updadpcm[%d].base == NULL\n", num, num));
  732.         return 0x00;
  733.     }
  734.  
  735. #if VERBOSE
  736.     if (!(offs&0xff)) LOG(1, ("UPD7759#%d sample offset = $%04x\n", num, offs));
  737. #endif
  738.  
  739.     return voice->base[offs];
  740. }
  741.  
  742. /************************************************************
  743.  UPD7759_busy_r
  744.  
  745.  !BUSY pin:
  746.  !BUSY outputs the status of the uPD7759. It goes low during
  747.  speech decode and output operations. When !ST is received,
  748.  !BUSY goes low. While !BUSY is low, another !ST will not be
  749.  accepted. In standby mode, !BUSY becomes high impedance. This
  750.  is an active low output.
  751.  *************************************************************/
  752.  
  753. int UPD7759_busy_r (int num)
  754. {
  755.     struct UPD7759voice *voice = updadpcm + num;
  756.  
  757.     /* If there's no sample rate, return not busy */
  758.     if ( Machine->sample_rate == 0 )
  759.         return 1;
  760.  
  761.     /* range check the numbers */
  762.     if( num >= upd7759_intf->num )
  763.     {
  764.         LOG(1,("error: UPD7759_busy_r() called with channel = %d, but only %d channels allocated\n", num, upd7759_intf->num));
  765.         return 1;
  766.     }
  767.  
  768.     /* bring the chip in sync with the CPU */
  769.     stream_update(channel[num], 0);
  770.  
  771.     if ( voice->playing == 0 )
  772.     {
  773.         LOG(1,("uPD7759 not busy\n"));
  774.         return 1;
  775.     }
  776.     else
  777.     {
  778.         LOG(1,("uPD7759 busy\n"));
  779.         return 0;
  780.     }
  781.  
  782.     return 1;
  783. }
  784.  
  785. /************************************************************
  786.  UPD7759_reset_w
  787.  
  788.  !RESET pin:
  789.  The !RESET input initialized the chip. Use !RESET following
  790.  power-up to abort speech reproduction or to release standby
  791.  mode. !RESET must remain low at least 12 oscillator clocks.
  792.  At power-up or when recovering from standby mode, !RESET
  793.  must remain low at least 12 more clocks after clock
  794.  oscillation stabilizes.
  795.  *************************************************************/
  796.  
  797. void UPD7759_reset_w (int num, int data)
  798. {
  799.     struct UPD7759voice *voice = updadpcm + num;
  800.  
  801.     /* If there's no sample rate, do nothing */
  802.     if( Machine->sample_rate == 0 )
  803.         return;
  804.  
  805.     /* range check the numbers */
  806.     if( num >= upd7759_intf->num )
  807.     {
  808.         LOG(1,("error: UPD7759_reset_w() called with channel = %d, but only %d channels allocated\n", num, upd7759_intf->num));
  809.         return;
  810.     }
  811.  
  812.     /* if !RESET is high, do nothing */
  813.     if (data > 0)
  814.         return;
  815.  
  816.     /* mark the uPD7759 as NOT PLAYING */
  817.     /* (Note: do we need to do anything else?) */
  818.     voice->playing = 0;
  819. }
  820.  
  821.  
  822. /* helper functions to be used as memory read handler function pointers */
  823. WRITE_HANDLER( UPD7759_0_message_w )    { UPD7759_message_w(0,data); }
  824. WRITE_HANDLER( UPD7759_0_start_w )    { UPD7759_start_w(0,data); }
  825. READ_HANDLER( UPD7759_0_busy_r )    { return UPD7759_busy_r(0); }
  826. READ_HANDLER( UPD7759_0_data_r )    { return UPD7759_data_r(0,offset); }
  827. READ_HANDLER( UPD7759_1_data_r )    { return UPD7759_data_r(1,offset); }
  828.